package vikings.aps;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import DBman.DatabaseManager;
import DBman.User;
import vikings.encrypter.Encrypt;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class UserRegistration extends HttpServlet implements Servlet{
	public void init(ServletConfig config) throws ServletException{
		super.init(config);
		
	}
	//process HTTP Get request
	public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException{
		Encrypt encrypt;
		DataValidation dval;
		try {
			DatabaseManager dbm=new DatabaseManager();
			
			//get data entered on the form
		    String surname=req.getParameter("surname");
		    String firstname=req.getParameter("firstname");
		    String email=req.getParameter("email");
		    String idNum=req.getParameter("idNum");
		    String telephone=req.getParameter("telephone");
		    String dob=req.getParameter("dob");
		    String username=req.getParameter("username");
		    String password=req.getParameter("password");
		    String confirmPassword=req.getParameter("confirmPassword");
		    
		    encrypt=new Encrypt();
		    encrypt.setPassword(password);
			String encryptedPassword=encrypt.getPassword();
			
			encrypt.setPassword(dob);
			String encryptedDOB=encrypt.getPassword();
			
			encrypt.setPassword(confirmPassword);
			String encryptedConfirmPassword=encrypt.getPassword();
			
			
			User user=dbm.registerUser(surname, encryptedPassword);
			//System.out.println(user.getSitePW());
			//System.out.println(user.getName());
			
			//check passwords
			if(encryptedPassword.equals(encryptedConfirmPassword)){
				getServletContext().getRequestDispatcher("/RegSuccess.jsp").forward(req, res);
				
			}
			else
			{
				JOptionPane.showMessageDialog(null, "Passwords do not match, please match them",
						 "Confirm Password!!", 1);

				String message = "Password do not match, please try again!.";
				req.getSession().setAttribute("can't register", message);
	          	getServletContext().getRequestDispatcher("/reg.jsp").forward(req, res);
			}
			
			dval=new DataValidation();
			boolean validEmail=dval.isEmailValid(email);
			boolean validPhone=dval.isPhoneNumberValid(telephone);
			
			if(!validEmail)
				JOptionPane.showMessageDialog(null, "Invalid email address","Re-enter the email address",1);
			if(!validPhone)
				JOptionPane.showMessageDialog(null, "Invalid Phone Number","Re-enter phone number",1);
				
				
					
					
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}  
	}
	/*public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
		
		
		System.out.println("---Output----");
	}*/
}
